﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;
using System.Collections;

namespace WebApplication
{
    public class BasePage : System.Web.UI.Page//,System.Web.UI.UserControl
    {
        public BasePage()
        { }

        public static BasePage GetInstance()
        {
            return new BasePage();
        }

        public string Create_Guid()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }

        public void CustomerNotLoginOption()
        {
            Session[Constant.SessionName.NextUrl] = Request.Url.PathAndQuery.ToString();
            Response.Redirect(Constant.MainPage.CustomerLogin);
        }

        #region Session操作
        
        /// <summary>
        /// 判断管理员是否登录
        /// </summary>
        /// <returns>true/false</returns>
        public bool IsManageLogin()
        {
            bool ret = false;
            if (GetManageFromSession() != null)
                ret = true;
           return ret;
        }
        
        /// <summary>
        /// 判断Customer是否登录
        /// </summary>
        /// <returns>true/false</returns>
        public bool IsCustomerLogin()
        {
            bool ret = false;
            if (GetCustomerFromSession() != null)
                ret = true;
            return ret;
        }
        /// <summary>
        /// 获取管理员Session值
        /// </summary>
        /// <returns>tblUser</returns>
        public tblUser GetManageFromSession()
        {
            tblUser ret=null;
            if (Session[Constant.SessionName.Manage] != null)
                ret= Session[Constant.SessionName.Manage] as tblUser;
            return ret;
        }

        public int GetMemberLoginUID()
        {
            int i = 0;
            if (Session[Constant.SessionName.MemberLoginUID] != null)
            {
                int.TryParse(Session[Constant.SessionName.MemberLoginUID].ToString(),out i);
            }
            #region member测试用的,暂时存放数据
            if (i == 0) {
                i = 1231;
            }
            #endregion
            return i;
        }
        public void SetMemberLoginUIDToSession(int LoginUID)
        {
            Session[Constant.SessionName.MemberLoginUID] = LoginUID;
        }

        /// <summary>
        /// 获取Customer Session值
        /// </summary>
        /// <returns>tblCustomer</returns>
        public tblCustomer GetCustomerFromSession()
        {
            tblCustomer ret = null;
            if (Session[Constant.SessionName.Customer] != null)
            {
                ret = Session[Constant.SessionName.Customer] as tblCustomer;
            }
            #region 测试
            else
            {
                tblCustomerBLL bll = new tblCustomerBLL();
                ret = bll.GetEntity(1268);
                SetCustomerToSession(ret);
            }
            #endregion
            return ret;
        }

        /// <summary>
        /// 从Session中获取URL，并销毁当前Session
        /// </summary>
        /// <returns></returns>
        public string GetUrlFromSession()
        {
            string ret = string.Empty;
            if (Session[Constant.SessionName.NextUrl] != null)
            {
                ret = Session[Constant.SessionName.NextUrl].ToString();
                Session.Remove(Constant.SessionName.NextUrl);
            }
            return ret;
        }
        
        /// <summary>
        /// 设置管理员Session
        /// </summary>
        /// <param name="ent">tblUser实体对象</param>
        public void SetManageToSession(tblUser ent)
        {
            Session[Constant.SessionName.Manage] = ent;
        }

        /// <summary>
        /// 设置Customer Session值
        /// </summary>
        /// <param name="ent"></param>
        public void SetCustomerToSession(tblCustomer ent)
        {
            Session[Constant.SessionName.Customer] = ent;
        }

        public void SetUrlToSession(string str)
        {
            Session[Constant.SessionName.Customer] = str;
        }
        #endregion

        /// <summary>
        /// 时间转换成 统一的格式  如：01 Jan 1955
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string DateFormat(object dt)
        {
            string ret = string.Empty;
            if (dt != null)
            {
                DateTime date = new DateTime();
                if (DateTime.TryParse(dt.ToString(), out date))
                    ret = date.ToString("dd MMM yyyy", new CultureInfo("en-US"));
            }
            return ret;
        }

        /// <summary>
        /// 将时间字符串转换成时间格式 Stirng=>DateTime
        /// </summary>
        /// <param name="dt">"01 Jan 1955" "2010-12-20"</param>
        /// <returns>DateTime</returns>
        public DateTime DateFormat(string dt)
        {
            DateTime date = new DateTime();
            if (DateTime.TryParse(dt.ToString(), out date))
                return date;
            return DateTime.MaxValue;
        }

        /// <summary>
        /// 时间转换成 统一的格式  如：01 Jan 1955 12:10:12
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string DateFormatString1(object dt)
        {
            string ret = string.Empty;
            if (dt != null)
            {
                DateTime date = new DateTime();
                if (DateTime.TryParse(dt.ToString(), out date))
                    ret = date.ToString("dd MMM yyyy hh:mm:ss", new CultureInfo("en-US"));
            }
            return ret;
        }

        /// <summary>
        /// 时间转换成 统一的格式  如：1955-01-01 12:10:12
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string DateFormatString2(object dt)
        {
            string ret = string.Empty;
            if (dt != null)
            {
                DateTime date = new DateTime();
                if (DateTime.TryParse(dt.ToString(), out date))
                    ret = date.ToString("yyyy-MM-dd hh:mm:ss", new CultureInfo("en-US"));
            }
            return ret;
        }

        

        protected string JoinPath(params object[] param)
        {
            string ret = string.Empty;
            for (int i = 0; i < param.Count(); i++)
                ret += param[i].ToString().Trim();
            return ret;
        }



        #region CustomerID的页面提示功能 
        /// <summary>
        /// 输入CustomerID提示的公共方法
        /// </summary>
        /// <param name="prefixText"></param>
        /// <param name="count"></param>
        /// <param name="contextKey"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCustomerIDList(string prefixText, int count, string contextKey)
        {
            tblCustomer entCustomer = new tblCustomer();
            tblCustomerBLL bllCustomer = new tblCustomerBLL();
            List<tblCustomer> listCustomer = bllCustomer.GetListByENT(entCustomer);
            var CustomerID = listCustomer.Select(p => p.CustomerID).Where(p => p.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase)).Take(10).ToArray();
            return CustomerID;
        }

        /// <summary>
        /// 输入CustomerName提示的公共方法
        /// </summary>
        /// <param name="prefixText"></param>
        /// <param name="count"></param>
        /// <param name="contextKey"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCustomerNameList(string prefixText, int count, string contextKey)
        {
            tblCustomer entCustomer = new tblCustomer();
            tblCustomerBLL bllCustomer = new tblCustomerBLL();
            List<tblCustomer> listCustomer = bllCustomer.GetListByENT(entCustomer);
            List<NewListResule> listTitle = new List<NewListResule>();
            List<NewListResule> listResule = new List<NewListResule>();
            NewListResule entNewList = null;
            foreach (tblCustomer item in listCustomer)
            {
                entNewList = new NewListResule();
                if (!String.IsNullOrEmpty(item.Name_Chi))
                {                    
                    entNewList.Title = item.Name_Chi;
                    listTitle.Add(entNewList);
                }
                entNewList = new NewListResule();
                if (!String.IsNullOrEmpty(item.Name_Eng)) {
                    entNewList.Title = item.Name_Eng;
                    listTitle.Add(entNewList);
                }
            }
            var equ = listTitle.Where(a => a.Title.Contains(prefixText)).ToList();
            foreach (var item in equ)
            {
                entNewList = new NewListResule();
                entNewList.Title = item.Title.ToString();
                listResule.Add(entNewList);
            }
            var val = listResule.Select(p => p.Title);
            //return val;
            var Customer =val.Where(p => p.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase)).Take(10).ToArray();
            //var Customer = val.Select(p => p.).Where(p => p.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase)).Take(10).ToArray();
            return Customer;
        }
        
        /// <summary>
        /// 输入Brand提示的公共方法
        /// </summary>
        /// <param name="prefixText"></param>
        /// <param name="count"></param>
        /// <param name="contextKey"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetBrandNameList(string prefixText, int count, string contextKey)
        {
            mtblBrand entBrand = new mtblBrand();
            mtblBrandBLL bllBrand = new mtblBrandBLL();
            List<mtblBrand> listBrand = bllBrand.GetListByENT(entBrand);
            List<NewListResule> listTitle = new List<NewListResule>();
            List<NewListResule> listResule = new List<NewListResule>();
            NewListResule entNewList = null;
            foreach (mtblBrand item in listBrand) {
                entNewList = new NewListResule();
                if (!String.IsNullOrEmpty(item.BrandName_Chi)) {
                    entNewList.Title = item.BrandName_Chi;
                    listTitle.Add(entNewList);
                }
                entNewList = new NewListResule();
                if (!String.IsNullOrEmpty(item.BrandName_Eng)) {
                    entNewList.Title = item.BrandName_Eng;
                    listTitle.Add(entNewList);
                }
            }
            var equ = listTitle.Where(a => a.Title.Contains(prefixText)).ToList();
            foreach (var item in equ) {
                entNewList = new NewListResule();
                entNewList.Title = item.Title.ToString();
                listResule.Add(entNewList);                    
            }
            var val = listResule.Select(p => p.Title);
            var Brand = val.Where(p => p.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase)).Take(10).ToArray();
            return Brand;

        }

        /// <summary>
        /// 输入Branch Name提示的公共方法
        /// </summary>
        /// <param name="prefixText"></param>
        /// <param name="count"></param>
        /// <param name="contextKey"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetBranchNameList(string prefixText, int count, string contextKey)
        {
            tblCust_Branch entBranch = new tblCust_Branch();
            tblCust_BranchBLL bllBranch = new tblCust_BranchBLL();
            List<tblCust_Branch> listBranch = bllBranch.GetListByENT(entBranch);
            List<NewListResule> listTitle = new List<NewListResule>();
            List<NewListResule> listResule = new List<NewListResule>();
            NewListResule entNewList = null;
            foreach (tblCust_Branch item in listBranch) {
                entNewList = new NewListResule();
                if (!String.IsNullOrEmpty(item.BranchName_Chi)) {
                    entNewList.Title = item.BranchName_Chi;
                    listTitle.Add(entNewList);
                }
                entNewList = new NewListResule();
                if (!String.IsNullOrEmpty(item.BranchName_Eng))
                {
                    entNewList.Title = item.BranchName_Eng;
                    listTitle.Add(entNewList);
                }
            }
            var equ = listTitle.Where(a => a.Title.Contains(prefixText)).Distinct().ToList();
            foreach (var item in equ)
            {
                entNewList = new NewListResule();
                entNewList.Title = item.Title.ToString();
                listResule.Add(entNewList);
            }
            var val = listResule.Select(p => p.Title);
            var Branch = val.Where(p => p.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase)).Take(10).ToArray();
            return Branch;
        }
        #endregion

        #region
        public int GetEnt()
        {
            int MemberUID = GetMemberLoginUID();
            int CustomerUID = 0;
            tblCust_WebLogin ent = new tblCust_WebLogin();
            ent= tblCust_WebLoginBLL.GetInstance().GetENT(MemberUID);
            if (ent != null) {
                CustomerUID = Convert.ToInt32(ent.CustomerUID);
            }
            return CustomerUID;
        }       
        #endregion
    }
}